package cn.ffcs.uec.insurance.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.insurance.action.bean.UecClassitemEditBean;
import cn.ffcs.uec.insurance.model.UecBmclass;
import cn.ffcs.uec.insurance.model.UecClassitem;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecClassitemEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -6096343177536382090L;

	private UecClassitemEditBean bean = new UecClassitemEditBean();

	private String opType;


	@Resource
	private IdentityCardConfigManager identityCardConfigManager;

	private UecClassitem uecClassitem;
	
	private UecBmclass uecBmclass;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
		
		
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecClassitemEditWin() throws Exception {
		bindCombobox();
		opType = (String) arg.get("opType");
		uecBmclass = (UecBmclass) arg.get("uecBmclass");
		if (opType.equals("add")) {
			bean.getUecClassitemEditWin().setTitle("新增");
			
		} else if (opType.equals("edit")) {
			
			bean.getUecClassitemEditWin().setTitle("修改");
			strToDate();
		} else if (opType.equals("view")) {
			bean.getUecClassitemEditWin().setTitle("查看");
			strToDate();
			PubUtil.disableComponent(bean.getUecClassitemEditWin());
		} 
	}
	

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffWorkData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
		uecClassitem = (UecClassitem) arg.get("uecClassitem");
		UecClassitem uecClassitemObj = UecClassitem.newInstance();
		if (uecClassitem != null) {
			PubUtil.fillPoFromEditorBean(uecClassitem, bean);
		} else {
			uecClassitem = uecClassitemObj;
			uecClassitem.setBmclassId(uecBmclass.getId());
			PubUtil.fillPoFromEditorBean(uecClassitem, bean);
		}
		
		if (opType.equals("add")) {
			// 保存员工
			/*String strBeginDate = DateUtil.dateToStr(this.bean.getBeginDate().getValue());
			uecClassitem.setBeginDate(strBeginDate);
			String strEndDate = DateUtil.dateToStr(this.bean.getEndDate().getValue());
			uecClassitem.setEndDate(strEndDate);
			String wbBeginDate = DateUtil.dateToStr(this.bean.getWbBeginDate().getValue());
			uecClassitem.setWbBeginDate(wbBeginDate);
			String wbEndDate = DateUtil.dateToStr(this.bean.getWbEndDate().getValue());
			uecClassitem.setWbEndDate(wbEndDate);*/
			uecClassitem.setStatusCd("1000");
			
			/*List<UecClassitem> uecClassitemsList = uecOutStaffManager.queryUecClassitem(uecBmclass);
			for (int i = 0; i < uecClassitemsList.size(); i++) {
				uecClassitemsList.get(i).updateOnly();
			}*/
			uecClassitem.addOnly();
			Events.postEvent("onOK", this.self, uecClassitem);
			onCancel();
			Messagebox.show("新增成功！");
		} else if (opType.equals("edit")) {
			/*String strBeginDate = DateUtil.dateToStr(this.bean.getBeginDate().getValue());
			uecClassitem.setBeginDate(strBeginDate);
			String strEndDate = DateUtil.dateToStr(this.bean.getEndDate().getValue());
			uecClassitem.setEndDate(strEndDate);
			String wbBeginDate = DateUtil.dateToStr(this.bean.getWbBeginDate().getValue());
			uecClassitem.setWbBeginDate(wbBeginDate);
			String wbEndDate = DateUtil.dateToStr(this.bean.getWbEndDate().getValue());
			uecClassitem.setWbEndDate(wbEndDate);*/
			uecClassitem.setStatusCd("1000");
			uecClassitem.updateOnly();
			Events.postEvent("onOK", this.self, uecClassitem);
			onCancel();
			Messagebox.show("修改成功！");
		}
		
		onCancel();
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecClassitemEditWin().onClose();
	}

	private String checkUacStaffWorkData() {
		// BaseInfo Check
		/*if (StrUtil.isNullOrEmpty(bean.getJobName().getValue())) {
			return "请填写岗位名称";
		} else if (StrUtil.checkBlank(bean.getJobName().getValue())) {
			return "岗位名称中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) {
			return "请填写在本单位从事外包开始日期";
		} */

		// IDCARD Real Name Check
		/*if (EnumUacStaffInfo.CERT_TYPE_IDCARD.getValue().equals(
				bean.getCertType().getSelectedItem().getValue())) {
			String idNum = bean.getCertNumber().getValue();
			String idName = bean.getCertName().getValue();
			try {
				boolean isRealName = CertUtil.checkIdCard(idNum, idName);

				if (!isRealName) {
					return "实名认证未通过";
				}
			} catch (Exception e) {
				return "调用国政通接口失败";
			}

		}*/

		return null;
	}
	/**
	 * 字符串转date
	 */
	private void strToDate() {
		if (arg.get("uecClassitem") != null) {
			uecClassitem = (UecClassitem) arg.get("uecClassitem");
			PubUtil.fillBeanFromPo(uecClassitem, bean);
			/*if (!StrUtil.isEmpty(uecOutStaffWork.getBeginDate())) {
				bean.getBeginDate().setValue(DateUtil.strToDate(uecOutStaffWork.getBeginDate()));
			}
			if (!StrUtil.isEmpty(uecOutStaffWork.getEndDate())) {
				bean.getEndDate().setValue(DateUtil.strToDate(uecOutStaffWork.getEndDate()));
			}
			if (!StrUtil.isEmpty(uecOutStaffWork.getWbBeginDate())) {
				bean.getWbBeginDate().setValue(DateUtil.strToDate(uecOutStaffWork.getWbBeginDate()));
			}
			if (!StrUtil.isEmpty(uecOutStaffWork.getWbEndDate())) {
				bean.getWbEndDate().setValue(DateUtil.strToDate(uecOutStaffWork.getWbEndDate()));
			}*/
		}
	}
	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
		String[][] isSend = {{"Y", "是"}, {"N", "否"}};
		for (int i = 0; i < isSend.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(isSend[i][j]);
				nodeVo.setName(isSend[i][j + 1]);
			}
			nodeVoIsSendList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getIstransfer(),nodeVoIsSendList);
		ListboxUtils.rendererForEdit(bean.getClearflag(),nodeVoIsSendList);
	}
}
